﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace LiteratureManage.Web.Controllers
{
    using LiteratureManage.Domain;
    using LiteratureManage.Domain.Services;

    public partial class DBBackupController : BaseController
    {
        private readonly IDBBackupService _service;

        public DBBackupController(IDBBackupService service)
        {
            this._service = service;
        }

        public virtual ActionResult Index()
        {
            var model = this._service.GetBackupFiles();
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Backup(DateTime theDate)
        {
            return CheckValidCall(() =>
            {
                this._service.Backup(theDate);
            })
            .Valid(() => this.Success("操作成功"))
            .Invalid(() => this.Failture("操作失败"));
        }

        [HttpPost]
        public virtual ActionResult Restore(string dbBackupFile)
        {
            return CheckValidCall(() =>
            {
                this._service.Restore(dbBackupFile);
            })
            .Valid(() => this.Success("操作成功"))
            .Invalid(() => this.Failture("操作失败"));
        }

        [HttpPost]
        public virtual ActionResult Delete(string dbBackupFile)
        {
            return CheckValidCall(() =>
            {
                this._service.Delete(dbBackupFile);
            })
            .Valid(() => this.Success("操作成功"))
            .Invalid(() => this.Failture("操作失败"));
        }

        public virtual ActionResult Download(string dbBackupFile)
        {
            var dbBackupFilePath = this._service.GetDBBackupFilePath(dbBackupFile);
            return File(dbBackupFilePath, "application/x-gzip", dbBackupFile);
        }
    }
}
